<?php

namespace Yolo\Service;

use Hyperf\Context\ApplicationContext;
use Hyperf\Validation\ValidatorFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * Validation
 * @author lushaoming<lushaoming6@gmail.com>
 * @date 2024/1/29
 */
class Validation
{
    /**
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $attributes
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function validate(array $data, array $rules, array $messages = [], array $attributes = []): array
    {
        $validator = ApplicationContext::getContainer()->get(ValidatorFactory::class)->make(
            $data,
            $rules,
            $messages,
            $attributes
        );

        if ($validator->fails()) {
            return $validator->messages()->all();
        }

        return [];
    }
}
